home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 October / Enter 10 2006.iso / Uzytki / Spiceworks Desktop 0.8.34 Beta / Spiceworks.exe / ruby / bin / gemwhich < prev    next >
Encoding:
Text File  |  2006-05-26  |  1.2 KB  |  62 lines

  1. #!/usr/bin/env ruby
  2.  
  3. require 'rubygems'
  4. require 'optparse'
  5.  
  6. EXT = %w( .rb .rbw .so .dll )
  7.  
  8. options = OptionParser.new
  9. options.banner = "gemwhich -- Find the location of a library module."
  10. options.separator("")
  11. options.separator("Usage: gemwhich [options] libname...")
  12. options.on_tail('-v', '--verbose',
  13.   "Enable verbose output"
  14.   ) do |value|
  15.   $verbose = value
  16. end
  17. options.on_tail('-h', '--help',
  18.   "Display this help message"
  19.   ) do |value|
  20.   puts options
  21.   exit
  22. end
  23. ARGV << '-h' if ARGV.empty?
  24.  
  25. begin
  26.   options.parse!(ARGV)
  27. rescue OptionParser::InvalidOption => ex
  28.   puts "Error: #{ex.message}"
  29.   exit
  30. end
  31.  
  32. def find_path(package_name, dirs)
  33.   dirs.each do |dir|
  34.     EXT.each do |ext|
  35.       full_path = File.join(dir, "#{package_name}#{ext}")
  36.       if File.exist?(full_path)
  37.     return full_path
  38.       end
  39.     end
  40.   end
  41.   nil
  42. end
  43.  
  44. searcher = Gem::GemPathSearcher.new
  45. ARGV.each do |arg|
  46.   dirs = $LOAD_PATH
  47.   spec = searcher.find(arg)
  48.   if spec
  49.     dirs =
  50.       spec.require_paths.collect { |d|
  51.       File.join(spec.full_gem_path,d)
  52.     } + $LOAD_PATH
  53.     puts "(checking gem #{spec.full_name} for #{arg})" if $verbose
  54.   end
  55.   path = find_path(arg, dirs)
  56.   if path
  57.     puts path
  58.   else
  59.     puts "Can't find #{arg}"
  60.   end
  61. end
  62.